package valve_test.DAO.Impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

import org.hibernate.Query;
import org.hibernate.Session;
import valve_test.util.HibernateUtil;
import valve_test.DAO.ValvesDAO;
import valve_test.logic.valves;
/**
 * Created with IntelliJ IDEA.
 * User: Shinigami
 * Date: 11.03.14
 * Time: 19:19
 * To change this template use File | Settings | File Templates.
 */
public class ValvesDAOImpl implements ValvesDAO{
    public void addValve(valves valve) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(valve);
            session.getTransaction().commit();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    public void updateValve(valves valve) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(valve);
            session.getTransaction().commit();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    public valves getValveById(Long id) throws SQLException {
        Session session = null;
        valves valve = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query query = session.createQuery(
                    " select valves from valves in class valves where valves.id = :id").setLong("id", id);
            List<valves> valvs = new ArrayList<valves>();
            valvs = query.list();
            if (valvs.size() > 0)
                valve = valvs.get(0);
            session.getTransaction().commit();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return valve;
    }

    public valves getValveByName(String name) throws SQLException {
        Session session = null;
        valves valve = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query query = session.createQuery(
                    " select valves from valves in class valves where valves.v_name = :v_name").setString("v_name", name);
            List<valves> valvs = new ArrayList<valves>();
            valvs = query.list();
            if (valvs.size() > 0)
                valve = valvs.get(0);
            session.getTransaction().commit();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return valve;
    }

    public List<valves> getAllValves() throws SQLException {
        Session session = null;
        List<valves> valvs = new ArrayList<valves>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            valvs = session.createCriteria(valves.class).list();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return valvs;
    }

    public void deleteValve(valves valve) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(valve);
            session.getTransaction().commit();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка I/O", JOptionPane.OK_OPTION);
            session.getTransaction().rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }


}
